package com.yilin.tms.capital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.tms.capital.mapper.TradeFlowMapper;
import com.yilin.tms.capital.service.ITradeFlowService;
import com.yilin.commons.util.FieldUtil;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.capital.TradeFlow;
import com.yilin.tms.core.entity.capital.TradeRecord;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.core.entity.user.organize.Organize;
import com.yilin.tms.core.remote.IUserRemote;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class TradeFlowService extends ServiceImpl<TradeFlowMapper, TradeFlow> implements ITradeFlowService {
    @Resource
    IUserRemote userRemote;
    @Resource
    TradeFlowMapper tradeFlowMapper;

    @Override
    public void saveOrUpdateBatchTradeFlow(List<TradeFlow> tradeFlowList) {
        saveOrUpdateBatch(tradeFlowList);
    }

    /**
     * 通交易单生成交易流水
     */
    @Override
    public void tradeRecordBySaveTradeFlow(TradeRecord tradeRecord) {
        if (tradeRecord.getTradeStatus() != TradeRecord.TradeStatus.paySuccess) return;
        List<TradeFlow> tradeFlows = new ArrayList<>();
        TradeFlow paymentTradeFlowOld = getTradeFlowByTradeNoType(tradeRecord.getTradeNo(), TradeFlow.PaymentType.expenditure);
        if (paymentTradeFlowOld == null) {
            TradeFlow paymentTradeFlow = new TradeFlow();
            paymentTradeFlow.setTransName(tradeRecord.getTransName());
            paymentTradeFlow.setTradeNo(tradeRecord.getTradeNo());
            paymentTradeFlow.setBankFlowNo(tradeRecord.getBankFlowNo());
            paymentTradeFlow.setPayType(tradeRecord.getPayType());
            paymentTradeFlow.setMoney(tradeRecord.getMoney());
            paymentTradeFlow.setUpper(TradeFlow.Upper.no);
            paymentTradeFlow.setPaymentType(TradeFlow.PaymentType.expenditure);
            paymentTradeFlow.setTradesDate(tradeRecord.getInitiateTime());
            paymentTradeFlow.setOrganizeId(tradeRecord.getPaymentOrganizeId());
            paymentTradeFlow.setOrganizeName(tradeRecord.getPaymentOrganizeName());
            paymentTradeFlow.setCapitalAccountNo(tradeRecord.getPaymentCapitalAccount());
            FieldUtil.copyFields(tradeRecord, paymentTradeFlow, null);//赋值平台ID
            tradeFlows.add(paymentTradeFlow);
        }
        TradeFlow incomeTradeFlowOld = getTradeFlowByTradeNoType(tradeRecord.getTradeNo(), TradeFlow.PaymentType.income);
        if (incomeTradeFlowOld == null) {
            TradeFlow incomeTradeFlow = new TradeFlow();
            incomeTradeFlow.setTransName(tradeRecord.getTransName());
            incomeTradeFlow.setTradeNo(tradeRecord.getTradeNo());
            incomeTradeFlow.setBankFlowNo(tradeRecord.getBankFlowNo());
            incomeTradeFlow.setPayType(tradeRecord.getPayType());
            incomeTradeFlow.setMoney(tradeRecord.getMoney());
            incomeTradeFlow.setUpper(TradeFlow.Upper.no);
            incomeTradeFlow.setPaymentType(TradeFlow.PaymentType.income);
            incomeTradeFlow.setTradesDate(tradeRecord.getInitiateTime());
            incomeTradeFlow.setOrganizeId(tradeRecord.getIncomeOrganizeId());
            incomeTradeFlow.setOrganizeName(tradeRecord.getIncomeOrganizeName());
            incomeTradeFlow.setCapitalAccountNo(tradeRecord.getIncomeCapitalAccount());
            Organize organize = ParamUtil.isSuccess(userRemote.getOrganizeById(tradeRecord.getIncomeOrganizeId()));
            if (organize.getUserType() == UserType.shipperUser || organize.getUserType() == UserType.truckerUser) {
                Organize organizePlat = ParamUtil.isSuccess(userRemote.getOrganizeById(organize.getSuperiorPlatOrganizeId()));
                incomeTradeFlow.setZonePlatId(organizePlat.getSuperiorPlatOrganizeId());
                incomeTradeFlow.setZonePlatName(organizePlat.getSuperiorPlatOrganizeName());
                incomeTradeFlow.setPlatId(organizePlat.getId());
                incomeTradeFlow.setPlatName(organizePlat.getOrganizeName());
            } else if (organize.getUserType() == UserType.platformUser) {
                incomeTradeFlow.setZonePlatId(organize.getSuperiorPlatOrganizeId());
                incomeTradeFlow.setZonePlatName(organize.getSuperiorPlatOrganizeName());
                incomeTradeFlow.setPlatId(organize.getId());
                incomeTradeFlow.setPlatName(organize.getOrganizeName());
            } else if (organize.getUserType() == UserType.zoneUser) {
                incomeTradeFlow.setZonePlatId(organize.getId());
                incomeTradeFlow.setZonePlatName(organize.getOrganizeName());
            }
            tradeFlows.add(incomeTradeFlow);
        }
        if (tradeFlows.size() > 0) saveOrUpdateBatchTradeFlow(tradeFlows);
    }

    /**
     * 根据本地流水号和类型查询流水
     */
    @Override
    public TradeFlow getTradeFlowByTradeNoType(String tradeNo, TradeFlow.PaymentType paymentType) {
        QueryWrapper<TradeFlow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("trade_no", tradeNo);
        queryWrapper.eq("payment_type", paymentType.index);
        return tradeFlowMapper.selectOne(queryWrapper);
    }


}
